package com.sh.shanhuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sh.shanhuo.R;
import com.sh.shanhuo.bean.BoxSearchBean;
import com.sh.shanhuo.bean.ToolBoxContentBean;
import com.sh.shanhuo.database.web.WebInfoDBUtils;
import com.sh.shanhuo.database.web.WebInfoEntity;

import java.util.List;

public class SearchBoxAdapter extends BaseQuickAdapter<BoxSearchBean.DataBean, BaseViewHolder> {

    private final Context context;

    public SearchBoxAdapter(Context context, List<BoxSearchBean.DataBean> filterNames) {
        super(R.layout.toolbox_view, filterNames);
        this.context = context;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void convert(BaseViewHolder helper, BoxSearchBean.DataBean item) {
        //ImageView already = helper.getView(R.id.already);
        ImageView boxLogo = helper.getView(R.id.boxLogo);
        ImageView addBox = helper.getView(R.id.addBox);
        TextView boxName = helper.getView(R.id.boxName);
        TextView boxContent = helper.getView(R.id.boxContent);
        boxName.setText(item.getWebName());
        boxContent.setText(item.getIntroduce());
        Glide.with(context)
                .load(item.getLogoUrl())
                .placeholder(R.drawable.icon_content)
                .error(R.drawable.icon_content)
                .into(boxLogo);
        WebInfoEntity webInfoEntity = WebInfoDBUtils.getWebInfoByWebId(context, item.getId());
        if (webInfoEntity != null) {
            addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
            //already.setVisibility(View.VISIBLE);
        } else {
            addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_add, null));
            //already.setVisibility(View.GONE);
        }
        addBox.setOnClickListener(v -> {
            if (webInfoEntity == null) {
                WebInfoEntity webInfoEntity1 = new WebInfoEntity();
                webInfoEntity1.webId = item.getId();
                webInfoEntity1.description = item.getIntroduce();
                webInfoEntity1.sortName = "默认";
                webInfoEntity1.onTop = 0;
                webInfoEntity1.serverTop = item.getTop();
                webInfoEntity1.isInServer = 1;
                webInfoEntity1.fromBox = 1;
                webInfoEntity1.webLink = item.getWebUrl();
                webInfoEntity1.webName = item.getWebName();
                webInfoEntity1.icon = item.getLogoUrl();
                webInfoEntity1.updateTime = System.currentTimeMillis();
                WebInfoDBUtils.insertWebInfo(context, webInfoEntity1);
                addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
                //already.setVisibility(View.VISIBLE);
            }
        });
    }
}
